//
//  SNSportTableViewCell.m
//  project
//
//  Created by 张淋 on 2019/6/1.
//  Copyright © 2019 黄建华. All rights reserved.
//

#import "SNSportTableViewCell.h"

@implementation SNSportTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupModel:(SportModeModel *)model selectType:(SelectDateType)type {
    //0x00: 非运动 模式(默认值)
    //0x01: 健步 0x02:跑步  0x03:登山  0x09: 跑步机 0x07: 足球   开始时间+运动时长+卡路里+距离
    //0x04: 骑行 0x05: 乒乓球 0x06: 篮球 0x08: 羽毛球 0x0A: 网球  开始时间+运动时长+平均心率+最大心率
    //0x0B: 游泳
    if (model.sportMode == 0) {
        self.sportTypeImageView.image = [UIImage image:@""];
    } else if (model.sportMode == 1) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_jianbu"];
        self.sportTypeDesLabel.text = SN_Text(@"健步");
    } else if (model.sportMode == 2) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_paobu"];
        self.sportTypeDesLabel.text = SN_Text(@"跑步");
    } else if (model.sportMode == 3) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_dengshan"];
        self.sportTypeDesLabel.text = SN_Text(@"登山");
    } else if (model.sportMode == 4) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_qixing"];
        self.sportTypeDesLabel.text = SN_Text(@"骑行");
    } else if (model.sportMode == 5) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_ppq"];
        self.sportTypeDesLabel.text = SN_Text(@"乒乓球");
    } else if (model.sportMode == 6) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_lanqiu"];
        self.sportTypeDesLabel.text = SN_Text(@"篮球");
    } else if (model.sportMode == 7) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_zuqiu"];
        self.sportTypeDesLabel.text = SN_Text(@"足球");
    } else if (model.sportMode == 8) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_ymq"];
        self.sportTypeDesLabel.text = SN_Text(@"羽毛球");
    } else if (model.sportMode == 9) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_paobuji"];
        self.sportTypeDesLabel.text = SN_Text(@"跑步机");
    } else if (model.sportMode == 10) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport1_wangqiu"];
        self.sportTypeDesLabel.text = SN_Text(@"网球");
    } else if (model.sportMode == 11) {
        self.sportTypeImageView.image = [UIImage image:@"icon_sport2_youyong"];
        self.sportTypeDesLabel.text = SN_Text(@"游泳");
    } else {
        self.sportTypeImageView.image = [UIImage image:@""];
    }
    
    
    if (model.sportMode == 1 || model.sportMode == 2 || model.sportMode == 3 || model.sportMode == 7 || model.sportMode == 9) {
        self.sportMiddleDesLabel.text = SN_Text(@"卡路里");
        self.sportMiddleImageView.image = [UIImage image:@"icon_xiangqing_cal"];
        self.sportMiddleTimeLabel.attributedText = [self label1With:[NSString stringWithFormat:@"%0.2f%@",model.calories / 1000.00,SN_Text(@"大卡")] withFontsize:16 withChangeText1:SN_Text(@"大卡")];
        self.sportRightImageView.image = [UIImage image:@"icon_xiangqing_juli"];
        self.sportRightDesLabel.text = SN_Text(@"距离");
        NSString *aDStr        = SN_USER.isMetricSystem ? [NSString stringWithFormat:@"%.2f %@", roundf(model.distance / 1000.00*0.0006214*100)/100, SN_Text(@"英里")] : [NSString stringWithFormat:@"%.2f %@", model.distance / 1000.00 , SN_Text(@"千米")];
        NSString *unit = SN_USER.isMetricSystem ? SN_Text(@"英里") : SN_Text(@"千米");
        self.sportRightTimeLabel.attributedText = [self label1With:aDStr withFontsize:16 withChangeText1:unit];
    } else if (model.sportMode == 4 || model.sportMode == 5 || model.sportMode == 6 || model.sportMode == 8 || model.sportMode == 10 || model.sportMode == 11) {
        self.sportMiddleDesLabel.text = SN_Text(@"平均心率");
        self.sportMiddleImageView.image = [UIImage image:@"icon_xiangqing_xlavg"];
        self.sportMiddleTimeLabel.attributedText = [self label1With:[NSString stringWithFormat:@"%ld%@",(long)model.heartAve,@"bpm"] withFontsize:16 withChangeText1:@"bpm"] ;
        self.sportRightImageView.image = [UIImage image:@"icon_xiangqing_xlmax"];
        self.sportRightDesLabel.text = SN_Text(@"最大心率");
        self.sportRightTimeLabel.attributedText = [self label1With:[NSString stringWithFormat:@"%ld%@",(long)model.heartMax,@"bpm"] withFontsize:16 withChangeText1:@"bpm"];
    } else {
        
    }
    
    NSString *totalTime = [self timeFormatted:(int)model.timeLength * 60];
    if ([totalTime contains:@"h"]) {
        self.sportTimeLabel.attributedText = [SN_PublicClass setChartViewCenterText:totalTime allTextFontSize:16 AttributesTextFontSize:16];
    } else {
        // 时 、分
        self.sportTimeLabel.attributedText = [SN_PublicClass attributed:totalTime withFontsize:16 withChangeText1:@"mins"];
    }
    self.sportTimeDesLabel.text = SN_Text(@"时长");
    
    if (type == SelectDateWeek) {
        NSArray *array = [model.startTime componentsSeparatedByString:@" "];
        self.sportDateLabel.text = [NSString stringWithFormat:@"%@ %@ %@",array[0],[NSDate weekDayStr:array[0] isInterNotional:YES],array[1]];
    } else {
        self.sportDateLabel.text = [NSString stringWithFormat:@"%ld%@",(long)model.sportDataNums,SN_Text(@"次")];
    }
}

- (NSString *)timeFormatted:(int)totalSeconds{
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    NSString *time = @"";
    if (hours == 0) {
        time = [NSString stringWithFormat:@"%02d%@", minutes,@"mins"];
    } else {
        time = [NSString stringWithFormat:@"%02d%@%02d%@",hours,@"h", minutes,@"mins"];
    }
    return time;
}

// 设置多段字体 参数1 字号  参数2 改变文字2 参数3 改变文字2
- (NSMutableAttributedString *)label1With:(NSString *)text withFontsize:(CGFloat)fontsize withChangeText1:(NSString *)text1 {
    NSMutableAttributedString *noteStr ;
    if (!text) {
        return noteStr;
    }
    noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (text1.length > 0) {
        // 需要改变的区间
        NSRange range1 = NSMakeRange(0, text.length - text1.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-Bold" size:fontsize] range:range1];
    }
    return noteStr;
}


@end
